import { findOrgMember } from '@/api/userCenter/authority/organization'
import { formatPagination, formatRequestPage } from '@/utils/antdOpFormatHelper'
import { reactive } from 'vue'
import { MemberPreviewDialogState } from '../types'

const useMemberPreviewDialog = () => {
  const memberDialogState = reactive<MemberPreviewDialogState>({
    visiable: false,
    tableData: [],
    currentId: '',
    pageInfo: {
      current: 1,
      pageSize: 20,
      total: 0,
    },
    tableColumn: [
      {
        title: 'ID',
        dataIndex: 'id',
      },
      {
        title: '用户名',
        dataIndex: 'username',
      },
      {
        title: '呢称',
        dataIndex: 'name',
      },
      {
        title: '手机号',
        dataIndex: 'phone',
      },
      {
        title: '邮箱',
        dataIndex: 'mail',
      },
      {
        title: '登录次数',
        dataIndex: 'loginTime',
      },
      {
        title: '最后登陆时间',
        dataIndex: 'loginTime',
      },
    ],
  })

  const getOrgMenmberByPage = async () => {
    const { data } = await findOrgMember(
      memberDialogState.currentId,
      formatRequestPage(memberDialogState.pageInfo)
    )
    if (data) {
      memberDialogState.tableData = data.records
      memberDialogState.pageInfo = formatPagination(data)
    } else {
      memberDialogState.tableData = []
      memberDialogState.pageInfo.current = 1
      memberDialogState.pageInfo.pageSize = 20
      memberDialogState.pageInfo.total = 0
    }
  }

  const openMemberDialog = async (orgId: string) => {
    memberDialogState.currentId = orgId
    await getOrgMenmberByPage()
    memberDialogState.visiable = true
  }

  const closeMemberDialog = () => {
    memberDialogState.tableData = []
  }

  return {
    memberDialogState,
    openMemberDialog,
    closeMemberDialog,
    getOrgMenmberByPage,
  }
}

export default useMemberPreviewDialog
